<!DOCTYPE html>
<html>
<head>
    <script>
        var app = parent.app;
        var context = parent.context;
        var pageParm = parent.pageParm;
        parent.pageParm = {};
    </script>
    <style>
        html, body {
            background-color: transparent !important;
            overflow-x: hidden;
            overflow-y: auto;
        }
    </style>
    <title data-culture="articleManagment">信息发布</title>
    <link href="/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/css/font-sans.min.css" rel="stylesheet" type="text/css"/>
    <script>
        var csss = ['/assets/css/page.css',
            '/assets/css/bootstrap-datetimepicker.css',
            '/assets/css/jquery.bootgrid.css',
            '/assets/css/route.css'
        ];
        for (m = 0; m < csss.length; m++) {
            document.write('<link href="' + csss[m] + '?license=' + new Date().getTime() + '" rel="stylesheet" />');
        }
    </script>
    <link href="/assets/css/typicons.min.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/css/animate.min.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/css/dataTables.bootstrap.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/js/ztree/css/metroStyle/metroStyle.css" rel="stylesheet" type="text/css"/>
    <script src="/assets/js/jquery.min.js" type="text/javascript"></script>
    <script src="/assets/js/jquery.cookie.js"></script>
    <script src="/assets/js/jquery-1.10.2.plugin.js"></script>
    <script src="/assets/js/culture.js"></script>
    <script src="/assets/js/bootstrap.min.js" type="text/javascript"></script>
    <script src="/assets/js/moment.js" type="text/javascript"></script>
    <script src="/assets/js/ztree/jquery.ztree.all-3.5.js"></script>
    <script src="/assets/js/selectpicker/jquery.selectpicker.js"></script>
    <script src="/assets/js/validation/jquery.bootvalidate.js"></script>
    <script src="/assets/js/slimscroll/jquery.slimscroll.min.js" type="text/javascript"></script>
    <script type="text/javascript" charset="utf-8" src="/assets/js/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="/assets/js/ueditor/ueditor.all.min.js"></script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="/assets/js/ueditor/lang/zh-cn/zh-cn.js"></script>
    <script src="/assets/js/skins.min.js"></script>
</head>
<body>
<div style="height: 100%;width: 100%; overflow-x: hidden;overflow-y: auto;">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12 col-sm-12 col-xs-12">
                <div class="widget  no-padding">
                    <div class="widget-header bg-themeprimary">
                        <i class="widget-icon typcn typcn-th-list"></i>
                        <span class="widget-caption" data-culture="articleList">信息发布</span>
                        <div class="toolbar">
                            <a id="btn_Save">
                                <i class="fa fa-location-arrow"></i>发布
                            </a>
                            <a id="btnBack">
                                <i class="fa fa-chevron-left"></i>返回
                            </a>
                        </div>
                    </div>
                    <div class="widget-body">
                        <div>
                            <form class="form-horizontal" role="form" id="form_article">
                                <div class="form-group">
                                    <label class="col-sm-1 control-label no-padding-right" for="title">标题</label>
                                    <div class="col-sm-11">
                                        <input class="form-control" id="title" name="title" type="text"
                                               placeholder="标题 ">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label no-padding-right" for="keywords">关键字</label>
                                    <div class="col-sm-11">
                                        <input class="form-control" id="keywords" name="keywords" type="text"
                                               placeholder="关键字">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label no-padding-right" for="org"><a
                                            onclick="choose()">机构</a></label>
                                    <div class="col-sm-11">
                                             <textarea class="form-control" id="org" name="org" required="" rows="1"
                                                       readonly="readonly"></textarea>
                                    </div>

                                </div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label no-padding-right" for="categoryId">类别</label>
                                    <div class="col-sm-2">
                                        <select class="form-control" id="categoryId" name="categoryId"></select>
                                    </div>
                                    <label class="col-sm-1 control-label no-padding-right"
                                           for="coment_type">是否评论</label>
                                    <div class="col-sm-2">
                                        <select class="form-control" id="coment_type" name="coment_type">
                                            <option value="0">禁止评论</option>
                                            <!--<option value="1">匿名评论</option>-->
                                            <option value="2">实名评论</option>
                                        </select>
                                    </div>

                                </div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label no-padding-right" for="editor">附件</label>
                                    <div class="col-sm-11">

                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label no-padding-right" for="editor">内容</label>
                                    <div class="col-sm-11">
                                        <script id="editor" type="text/plain" style="height:360PX; width:100%;">
                                        </script>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel"
     aria-hidden="true" id="orgTreeModal">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="widget">
                <div class="widget-header bg-themeprimary">
                    <i class=" widget-icon glyphicon glyphicon-book"></i>
                    <span class="widget-caption">选择机构</span>
                    <div class="widget-buttons">
                        <a data-dismiss="modal">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="widget-body">
                    <ul id="orgTree" class="ztree" style="width:auto; overflow:auto;"></ul>
                </div>
                <div class="modal-footer">
                    <button type="button" id="btnChooseOrg" class="btn btn-success">
                        确定
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var articleObj = {
        insert: (pageParm != undefined && pageParm.form != undefined) ? false : true,
        ue: UE.getEditor('editor'),
        currentArticle: (pageParm != undefined && pageParm.form != undefined) ? pageParm.form : {},
        orgTreeObj: {},
        audit: [],
        validateParam: {
            submitBtn: $("#btn_Save"),
            submitHandler: function (obj) {
                if (articleObj.audit.length == 0) {
                    parent.bootbox.alert("请选择机构！");
                    return;
                }
                obj.insert = articleObj.insert;
                if (!articleObj.insert) {
                    obj.id = articleObj.currentArticle.id;
                }
                obj.categoryName = $("#categoryId").find("option:selected").text();
                $('#btn_save').button('loading');
                obj.audits = articleObj.audit;
                obj.articleData = {articleId: obj.id, insert: obj.insert, content: escape(articleObj.ue.getContent())};
                $.request("/cms/article/save", obj, function (result) {
                    parent.loadpage('views/cms/article.html')
                }, function () {
                    parent.bootbox.alert('保存失败！');
                });
            },
            fields: {
                title: {
                    validators: {
                        notEmpty: {}
                    }
                }, coment_type: {
                    validators: {
                        notEmpty: {}
                    }
                }, categoryId: {
                    validators: {
                        notEmpty: {}
                    }
                }
            }
        }
    }
    function initOrg(articleId) {
        var checkeds = [];
        $.request('/cms/article/audit/query/articleid', {articleId: articleId, auditType: 0}, function (result) {
            $.each(result, function (i, v) {
                checkeds.push(v.auditId);
            });
        }, false, false)
        articleObj.orgTreeObj = $('#orgTree').tree({
            data: {
                key: {name: 'orgName', enname: 'orgEnName'},
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "parentId",
                    rootPId: 0
                }
            },
            check: {
                enable: true
            },
            checkdata: checkeds,
            expandLevel: 3
        });
        $('#btnChooseOrg').unbind('click').click(function () {
            var nodes = articleObj.orgTreeObj.getCheckedNodes(true);
            var value = "";
            articleObj.audit = [];
            $.each(nodes, function (i, item) {
                if (!item.isParent) {
                    articleObj.audit.push({
                        articleId: articleId,
                        auditId: item.id,
                        auditType: 0,
                        auditName: item.orgName
                    })
                    value += item.orgName + ";";
                }
            })
            $('#org').val(value)
            $('#orgTreeModal').modal('hide');
        })
    }
    function choose() {
        $('#orgTreeModal').modal('show');
    }
    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    function isFocus(e) {
        alert(UE.getEditor('editor').isFocus());
        UE.dom.domUtils.preventDefault(e)
    }
    function setblur(e) {
        UE.getEditor('editor').blur();
        UE.dom.domUtils.preventDefault(e)
    }
    function insertHtml() {
        var value = prompt('插入html代码', '');
        UE.getEditor('editor').execCommand('insertHtml', value)
    }
    function createEditor() {
        enableBtn();
        UE.getEditor('editor');
    }
    function getAllHtml() {
        alert(UE.getEditor('editor').getAllHtml())
    }
    function getContent() {
        var arr = [];
        arr.push("使用editor.getContent()方法可以获得编辑器的内容");
        arr.push("内容为：");
        arr.push(UE.getEditor('editor').getContent());
        alert(arr.join("\n"));
    }
    function getPlainTxt() {
        var arr = [];
        arr.push("使用editor.getPlainTxt()方法可以获得编辑器的带格式的纯文本内容");
        arr.push("内容为：");
        arr.push(UE.getEditor('editor').getPlainTxt());
        alert(arr.join('\n'))
    }
    function setContent(isAppendTo) {
        var arr = [];
        arr.push("使用editor.setContent('欢迎使用ueditor')方法可以设置编辑器的内容");
        UE.getEditor('editor').setContent('欢迎使用ueditor', isAppendTo);
        alert(arr.join("\n"));
    }
    function setDisabled() {
        UE.getEditor('editor').setDisabled('fullscreen');
        disableBtn("enable");
    }

    function setEnabled() {
        UE.getEditor('editor').setEnabled();
        enableBtn();
    }

    function getText() {
        //当你点击按钮时编辑区域已经失去了焦点，如果直接用getText将不会得到内容，所以要在选回来，然后取得内容
        var range = UE.getEditor('editor').selection.getRange();
        range.select();
        var txt = UE.getEditor('editor').selection.getText();
        alert(txt)
    }
    function getContentTxt() {
        var arr = [];
        arr.push("使用editor.getContentTxt()方法可以获得编辑器的纯文本内容");
        arr.push("编辑器的纯文本内容为：");
        arr.push(UE.getEditor('editor').getContentTxt());
        alert(arr.join("\n"));
    }
    function hasContent() {
        var arr = [];
        arr.push("使用editor.hasContents()方法判断编辑器里是否有内容");
        arr.push("判断结果为：");
        arr.push(UE.getEditor('editor').hasContents());
        alert(arr.join("\n"));
    }
    function setFocus() {
        UE.getEditor('editor').focus();
    }
    function deleteEditor() {
        disableBtn();
        UE.getEditor('editor').destroy();
    }
    function disableBtn(str) {
        var div = document.getElementById('btns');
        var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
        for (var i = 0, btn; btn = btns[i++];) {
            if (btn.id == str) {
                UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
            } else {
                btn.setAttribute("disabled", "true");
            }
        }
    }
    function enableBtn() {
        var div = document.getElementById('btns');
        var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
        for (var i = 0, btn; btn = btns[i++];) {
            UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
        }
    }
    function getLocalData() {
        alert(UE.getEditor('editor').execCommand("getlocaldata"));
    }

    function clearLocalData() {
        UE.getEditor('editor').execCommand("clearlocaldata");
        alert("已清空草稿箱")
    }
    function init() {
        $('#categoryId').selectpicker({
            data: {
                url: '/cms/category/query/parentId',
                parentId: 100
            }
        });
        if (pageParm.form) {
            $.request('/cms/article/query/articleid', {id: pageParm.form.id}, function (result) {
                if (result) {
                    $('#title').val(result.title);
                    $('#keywords').val(result.keywords);
                    $('#categoryId').val(result.categoryId);
                    $('#comentType').val(result.comentType);
                    var content = unescape(result.articleData.content)
                    articleObj.ue.ready(function () {
                        articleObj.ue.setContent(content, true)
                    });
                    var org = "";
                    articleObj.audit = result.audits;
                    $.each(result.audits, function (i, v) {
                        if (v.auditType == 0) {
                            org += v.auditName + ";";
                        }
                    })
                    $('#org').val(org)
                }
            }, false);
        }
        $('#form_article').validate(articleObj.validateParam);
    }
    $(function () {
        init();
        initOrg(articleObj.currentArticle.id);
    });
</script>
</body>
</html>